﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HGZP.BLL;
using HGZP.Model;
using HGZP.PLL;

namespace Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.Expires = 0;
            //Response.CacheControl = "no-cache";
            string TmUrl = Request.QueryString["urls"];
            if (!string.IsNullOrEmpty(TmUrl))
                this.HidUrl.Value = TmUrl.ToString();
            //检测是否开启安全码

            if (HGZP.PLL.BaseConfig.GetConfigValue("isProtRand", Server.MapPath("~/admin/Sys/xml/Sys.config")) == "0")
                trSafeCode.Visible = false;
        }


        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (Page.IsValid)
            {
                string Account = this.TxtName.Text;
                string PassWord = this.TxtPassword.Text;
                string VerifyCode = this.TxtVerify.Text.Trim();
                string SafeCode = this.TxtSafeCode.Text;
                string Urls = this.HidUrl.Value;
                if (Account.Trim() == string.Empty || PassWord.Trim() == string.Empty)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('用户与密码不能为空');</script>");
                    return;
                }

                #region 验证码
                if (Session["CheckCode"] == null)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('验证码过期，请返回重新登录!');</script>");
                    return;
                }
                string CheckCode = Session["CheckCode"].ToString().ToUpper();
                Session.Remove("CheckCode");
                if (VerifyCode.ToUpper() != CheckCode)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('输入验证码错误!');</script>");
                    return;
                }
                #endregion

                #region 安全码验证

                if (HGZP.PLL.BaseConfig.GetConfigValue("isProtRand", Server.MapPath("~/admin/Sys/xml/Sys.config")) == "1")
                    if (SafeCode != HGZP.PLL.BaseConfig.GetConfigValue("protRand", Server.MapPath("~/admin/Sys/xml/Sys.config")))
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('安全码不正确!');</script>");
                        return;
                    }
                #endregion 安全码验证

                BaseUserBLL baseuser = new BaseUserBLL();
                string result = baseuser.Exist(Account.Trim(),EncryptAndDecrypt.EncryptTripleDES(PassWord.Trim()));
                string[] results = result.Split(',');
                switch (results[0])
                {
                    case "4":
                        {
                            Response.Write("<script>alert('" + results[1] + "')</script>");
                            break;
                        }
                    case "1":
                        {
                            Response.Write("<script>alert('" + results[1] + "')</script>");
                            break;
                        }
                    case "2":
                        {
                            Response.Write("<script>alert('" + results[1] + "')</script>");
                            break;
                        }
                    default:
                        {
                            Session["user"] = Account;
                            Session["UserId"] = results[1];
                            if (string.IsNullOrEmpty(Urls))
                                Response.Redirect("admin.aspx");
                            else
                                Response.Redirect(Urls);
                            break;
                        }
                }

            }
        }


    }
}
